package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Collect;
import com.wm.bean.Restaurant;
import com.wm.bean.Scriptorium;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.CollectService;
import com.wm.service.RestaurantScriptoriumConService;
import com.wm.service.RestaurantService;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserService;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class ResListAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(ResListAction.class);
	
	private RestaurantService restaurantService;
	private RestaurantScriptoriumConService restaurantScriptoriumConService;
	private CollectService collectService;
	private ScriptoriumService scriptoriumService;

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}

	public CollectService getCollectService() {
		return collectService;
	}

	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}

	public RestaurantScriptoriumConService getRestaurantScriptoriumConService() {
		return restaurantScriptoriumConService;
	}

	public void setRestaurantScriptoriumConService(
			RestaurantScriptoriumConService restaurantScriptoriumConService) {
		this.restaurantScriptoriumConService = restaurantScriptoriumConService;
	}

	public RestaurantService getRestaurantService() {
		return restaurantService;
	}

	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}

	private Map session;
	
	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private long id;					//主键ID
	private long resId;					//饭店ID
	private List list;
	private int listSize;
	private String result;
	private List collectList;			//收藏列表
	private int more;					//是否有更多 0没有1有
	private long uid;					//用户id
	private String scrName;				//楼宇名字

	public String getScrName() {
		return scrName;
	}

	public void setScrName(String scrName) {
		this.scrName = scrName;
	}
	
	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}
	
	public int getMore() {
		return more;
	}

	public void setMore(int more) {
		this.more = more;
	}
	
	public List getCollectList() {
		return collectList;
	}

	public void setCollectList(List collectList) {
		this.collectList = collectList;
	}
	
	public int getListSize() {
		return listSize;
	}

	public void setListSize(int listSize) {
		this.listSize = listSize;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getResId() {
		return resId;
	}

	public void setResId(long resId) {
		this.resId = resId;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		if(this.getId()==0){
			//查cookie记录
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			UserCookie userCookie = new UserCookie(request,response);
			String scrId = userCookie.getCookie(Constant.SCR_COOKIE_KEY);
			if(scrId!=null && !scrId.equals("")){
				this.setId(Integer.valueOf(scrId));
				//查楼宇
				Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.getId());
				if(scriptorium!=null){
					this.setScrName(scriptorium.getScrName());
				}
			}else{
				return "indexAction";
			}
		}
		if(this.getId()>0){
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
				this.setUid(us.getUid());
			}
			
			//查询收藏
			if(this.getUid()>0){
				this.setCollectList(this.getCollectService().findCollectByUserIdObjTypeFirstResultMaxResults(this.getUid(),0, "id", 0, 0, 5));
			}
			
			//查询饭店
			this.setList(this.getRestaurantScriptoriumConService().findRestaurantScriptoriumConsByScrIdIsShowCollectList(this.getId(), 1, "orderNum", 0,this.getCollectList()));
			if(this.getList().size()>15){
				this.setMore(1);
			}
			
			//查楼宇
			Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.getId());
			if(scriptorium!=null){
				this.setScrName(scriptorium.getScrName());
			}
			
			//记录cookie
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			UserCookie userCookie = new UserCookie(request,response);
			String scrId = userCookie.getCookie(Constant.SCR_COOKIE_KEY);
			if(scrId!=null && !scrId.equals("")){
				if(this.getId() != Integer.valueOf(scrId)){
					userCookie.removeCookie(Constant.SCR_COOKIE_KEY);
					userCookie.addCookie(Constant.SCR_COOKIE_KEY, String.valueOf(this.getId()));
				}
			}else{
				userCookie.addCookie(Constant.SCR_COOKIE_KEY, String.valueOf(this.getId()));
			}
		}
		return "index";
	}
}
